﻿
@{
    ViewBag.Title = "TestFeeView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>协调费明细</legend>
</fieldset>
<form class="layui-form" enctype="multipart/form-data">
    <div class="layui-form-item">
        <label class="layui-form-label">搜索</label>
        <div class="layui-input-block">
            <input type="text" id="SearchText" name="Title1" required lay-verify="required" onkeypress="getKey()" placeholder="输入协调内容" class="layui-input" style="width:300px;float:left;">
            <input type="text" value="" style="display:none" />
            <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="SearchFunc()"><i class="layui-icon">&#xe615;</i></button>
            <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="BtnAdd"><i class="layui-icon">&#xe61f;</i>新增引入</button>
        </div>
    </div>
</form>
<table id="demo" lay-filter="PECoordFeeTablefilter"></table>

<div id="QuoteCoordFeeLayer" style="display:none;">
    <form class="layui-form" enctype="multipart/form-data">
        <div class="layui-form-item">
            <label class="layui-form-label">搜索</label>
            <div class="layui-input-block">
                <input type="text" id="QuoteSearchText" name="Title1" required lay-verify="required" onkeypress="QuoteGetKey()" placeholder="输入协调内容" class="layui-input" style="width:300px;float:left;">
                <input type="text" value="" style="display:none" />
                <button type="button" class="layui-btn layui-btn-normal" style="float:left;" onclick="QuoteSearchFunc()"><i class="layui-icon">&#xe615;</i></button>
                <button type="button" class="layui-btn layui-btn-danger" style="float:left;" id="QuoteBtnAdd"><i class="layui-icon">&#xe61f;</i>引入选中项</button>
            </div>
        </div>
    </form>
    <table id="CoordFeeTable" lay-filter="CoordFeeTablefilter"></table>
</div>


<script type="text/javascript">
    var table;
    var PreEstId = parent.$("#PreEstId").val();
    var PreEstType = parent.$("#ProjectType").val();
    layui.use(['form','table', 'upload', 'layer'], function () {
        var upload = layui.upload;
        table = layui.table;
        var tab = parent.tab;
        var layer = layui.layer;
        var form = layui.form;
        var index;
        var layerindex;
        var IsValidate = 0;



        $("#BtnAdd").on("click", function () {
            layerindex = layer.open({
                type: 1,
                title: ['试验费信息池', 'font-size:18px;'],
                area: '1000px',
                offset:'rt',
                content: $("#QuoteCoordFeeLayer"),  //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                success: function (layero, index) {
                    //$("#EditFrom input[name='TestFeeId']").val("");
                }
            });
        });

        $("#QuoteBtnAdd").on("click", function () {
            var checkStatus = table.checkStatus('CoordFeeTable');
            var data = checkStatus.data;    //这里得到的data是array类型
            //alert(JSON.stringify(data));
            //先检查内容
            data.forEach(function (item, index) {
                item["PreEstId"] = PreEstId;
                item["PreEstType"] = PreEstType;
            });
            //return;
            $.ajax({
                type: "POST",
                url: "/Estimate/CoordFeeAdd",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('引入成功', { icon: 6, time: 1000 });
                        table.reload('demo');
                    } else {
                        alert(msg.result);
                    }
                }
            });


        });




        //第一个实例
        table.render({
            elem: '#demo'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/PECoordFeeTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType+"'" }
            , limits: [10, 50, 100]
            , cols: [[ //表头
                 { field: 'CooContent', title: '协调内容', width: 150 }
                , { field: 'CooDepart', title: '协调部门', width: 200 }
                , { field: 'QuoteMoney', title: '金额', width: 100, edit: 'text'}
                , { field: 'Quantity', title: '数量', width: 80, edit: 'text'}
                , { field: 'Memo', title: '备注', width: 150, edit: 'text' }
                , {
                    title: '单价对比', width: 100,
                    templet: function (d) {
                        if (d.QuoteMoney > d.money ) {
                            return '<i class="layui-icon layui-icon-up" style="font-size:16px;color:#FF0000;"></i>' + (d.QuoteMoney - d.money).toFixed(0);
                        }
                        else if (d.QuoteMoney < d.money) {
                            return '<i class="layui-icon layui-icon-down" style="font-size: 16px; color: #238E23;"></i>' + (d.money - d.QuoteMoney).toFixed(0);
                        }
                        else {
                            return '--';
                        }
                    }
                }
                , {
                    field: "operation", title: "操作", width: 180, fixed: 'right',
                    templet: function (d) {
                        return '<a class="layui-btn layui-btn-xs" lay-event="TableDel">删除</a>';
                    }
                }
            ]]
        });



        table.on("tool(PECoordFeeTablefilter)", function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象


            if (layEvent == 'TableDel') {
                layer.confirm('<span class="layui-icon"></span>' + '是否真的要删除“' + data.CooContent + '？', {
                    btn: ['确定删除', '取消'] //可以无限个按钮
                    , btn1: function (index)
                    {
                        //按钮【删除】的回调
                        $.ajax({
                            type: "POST",
                            url: "/Estimate/PECoordFeeDel",
                            async: false,
                            data: { "CoordFeeId": data.CoordFeeId },
                            success: function (msg)
                            {
                                if (msg.result == "OK") {
                                    layer.msg('删除成功"', { icon: 6, time: 1000 });
                                    table.reload("demo", {

                                    });
                                } else {
                                    alert("删除失败");
                                }
                            }
                        });
                        layer.close(index);
                    }
                    , btn2: function (index)
                    {
                        //按钮【按钮三】的回调
                        //alert("2");
                    }
                });
            }

        });
        table.on('edit(PECoordFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  

            $.ajax({
                type: "POST",
                url: "/Estimate/PECoordFeeUpdate",
                async: false,
                contentType: "application/json",
                dataType: "json",
                data: JSON.stringify(obj.data),
                success: function (msg) {
                    if (msg.result == "OK") {
                        layer.msg('修改成功', { icon: 6, time: 1000 });
                        table.reload('demo');
                    } else {
                        alert(msg.result);
                    }
                }
            });
        });

        //原始协调费表
        table.render({
            elem: '#CoordFeeTable'
            , method: 'post'
            //, height: 315
            , url: '/Estimate/QuoteCoordFeeTableRequest' //数据接口
            , page: true //开启分页
            , where: { SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 " }
            , limits: [20, 50, 100]
            , cols: [[ //表头
                { type: 'checkbox' }
                , { field: 'CooContent', title: '协调内容', width: 150}
                , { field: 'CooDepart', title: '协调部门', width: 200}
                , { field: 'money', title: '预算金额', width: 150 }
                , { field: 'memo', title: '备注', width: 150 }
                , {
                    field: 'QuoteMoney', title: '引用金额', width: 100, edit: 'text'
                }
                , {
                    field: 'Quantity', title: '数量', width: 80, edit: 'text'                    
                }
            ]]
        });

        table.on('edit(CoordFeeTablefilter)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
            //console.log(obj.value); //得到修改后的值
            //console.log(obj.field); //当前编辑的字段名
            //console.log(obj.data); //所在行的所有相关数据  

            var data = obj.data;
            var field = obj.field;
            try {
                if (field == "QuoteMoney") {
                    eval(data.QuoteMoney);
                }
                if (field == "Quantity") {
                    eval(data.Quantity);
                }
            } catch (e) {
                layer.msg('数据填写格式有误', { icon: 2, time: 1500 });
                table.reload('CoordFeeTable');
                return;
            }
        });



    });

    //数据表搜索回车键
    function getKey() {
        if (event.keyCode == 13) {
            SearchFunc();
        }
    }
    function SearchFunc() {
        var text = $("#SearchText").val();
        if (text == "") {
            table.reload("demo", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                }
            });
        }
        else {
            table.reload("demo", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 and CooContent like '%" + text + "%' and PreEstId=" + PreEstId + " and PreEstType='" + PreEstType + "'"
                }
            });
        }
    }

    //引用表搜索
    function QuoteGetKey() {
        if (event.keyCode == 13) {
            QuoteSearchFunc();
        }
    }
    function QuoteSearchFunc() {
        var text = $("#QuoteSearchText").val();
        if (text == "") {
            table.reload("CoordFeeTable", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1"
                },
                page: {
                    curr: 1
                }
            });
        }
        else {
            table.reload("CoordFeeTable", {
                where: {
                    SortName: "CoordFeeId", Order: "Desc", Condition: " and IsDelete<>1 and CooContent like '%" + text + "%'"
                },
                page: {
                    curr: 1
                }
            });
        }
    }


    function dealNumber(money) {
        if (money && money != null) {
            money = String(money);
            var left = money.split('.')[0], right = money.split('.')[1];
            right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
            var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
            return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
        } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
            return '0.00';
        } else {
            return "";
        }
    };

</script>


